#include "play_state.h"
#include "game.h"
#include "game_over_state.h"
#include "input_handler.h"
#include "pause_state.h"
#include "ship_handler.h"
#include "state_parser.h"
#include "texture_manager.h"

const std::string PlayState::s_playID = "PLAY";

void PlayState::update()
{
    if (m_loadingComplete && !m_exiting)
    {
        if (TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_ESCAPE))
        {
            TheGame::Instance()->getStateMachine()->pushState(new PauseState());
        }
        if (!m_gameObjects.empty())
        {
            for (unsigned int i = 0; i < m_gameObjects.size(); i++)
            {
                if (m_gameObjects[i] != 0)
                {
                    m_gameObjects[i]->update();
                }
            }
        }
        TheShipHandler::Instance()->updateShips();
        if (TheShipHandler::Instance()->gameFinished() != 0) {
            if (TheShipHandler::Instance()->gameFinished() > 0)
                TheGame::Instance()->getStateMachine()->changeState(new GameOverState(true));
            else
                TheGame::Instance()->getStateMachine()->changeState(new GameOverState(false));
        }
    }    
}

void PlayState::render()
{
    if (m_loadingComplete && !m_gameObjects.empty())
    {
        for (unsigned int i = 0; i < m_gameObjects.size(); i++)
        {
            m_gameObjects[i]->draw();
        }
        TheTextureManager::Instance()->drawFrame("text", 87, 70, 452, 100, 0, 0, TheGame::Instance()->getRenderer(), 0, 255, 0.5);
        TheTextureManager::Instance()->drawFrame("text", 487, 70, 452, 100, 0, 1, TheGame::Instance()->getRenderer(), 0, 255, 0.5);
        TheTextureManager::Instance()->drawGrids();
        TheShipHandler::Instance()->drawShips();
    }
}

bool PlayState::onEnter()
{
    StateParser stateParser;
    stateParser.parseState("battleships.xml", s_playID, &m_gameObjects, &m_textureIDList);

    TheTextureManager::Instance()->load("images/text.png", "text", TheGame::Instance()->getRenderer());
    TheTextureManager::Instance()->load("images/ship1.png", "ship1", TheGame::Instance()->getRenderer());
    TheTextureManager::Instance()->load("images/ship2.png", "ship2", TheGame::Instance()->getRenderer());
    TheTextureManager::Instance()->load("images/ship3.png", "ship3", TheGame::Instance()->getRenderer());
    TheTextureManager::Instance()->load("images/ship4.png", "ship4", TheGame::Instance()->getRenderer());
    TheTextureManager::Instance()->load("images/ship5.png", "ship5", TheGame::Instance()->getRenderer());
    TheTextureManager::Instance()->load("images/hit.png", "hit", TheGame::Instance()->getRenderer());
    TheTextureManager::Instance()->load("images/miss.png", "miss", TheGame::Instance()->getRenderer());

    TheShipHandler::Instance()->addShips();
    
    m_loadingComplete = true;

    std::cout << "entering PlayState\n";
    return true;
}

bool PlayState::onExit()
{
    m_exiting = true;

    TheInputHandler::Instance()->reset();
    TheShipHandler::Instance()->clearShips();

    std::cout << "exiting PlayState\n";
    return true;
}
